$(function() {
    $("#table").tablesorter({ sortList: [[0,0], [1,0]] });
    $('th').css('cursor', 'hand');
});
//筛选弹框点击按钮不会自动关闭
$(document).on('click', '#sqlInputDiv .dropdown-menu', function (e) {
    e.stopPropagation();
});

$(document).ready(function (){
    //载入select2库
    let $select = $('.js-example-basic-single');
    $select.select2({
        dropdownParent: $('#backDropModal'),
        closeOnSelect: true
    });

    $("#searchInput").on("keyup", function() {
        let value = $(this).val().toLowerCase();
        $("#table tbody tr").filter(function() {
            $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
        });
    });
});
$('#delButton').on('click',function(e){
    e.preventDefault();
    let answer=confirm('真的要删除吗？');
    if(answer){
        del();
    }
});
$('button.sqlButton').on('click',function(){
    let sqv = $( this ).attr('sqlValue');
    $( "#sqlInput" ).val( function( index, val ) {
        return val + sqv;
    });
});
//设置表行可以点击
$(document).on('click', '.tableRow_clickable', function(){
    $('.jump2DetailFrameAble').remove();
    let tableName = $('#tableJs').attr('entityTableName');
    let lowTableName = uncapitalizeFirstLetter(tableName);
    let clzName = $('#tableJs').attr('entityClassName');
    let cond1 = tableName.endsWith('Report');
    //xxxReport/Stockxxx是各种单据
    let cond2 = tableName.startsWith('Stock');
    let cond3 = tableName.includes('Item');
    if((cond1 || cond2) && !cond3){
        let idValue = $(this).children('td[fieldName="id"]').text();
        let urlF =`CrudPreprocessServlet?entityClassName=${clzName}Item&condition=${lowTableName}_id=${idValue}`;

        $(this).find('td:last').after(`<td class="jump2DetailFrameAble"><button class="btn btn-info btn-xs" id="btn-click-jump-frame" onclick="jump2DetailFrame('${urlF}')"/><i class="fa fa-info"></i></td>`);

    }
        let isSelected = $(this).hasClass("highlight");
        $("#table tr").removeClass("highlight");
        toggleButtons(true);

        if (!isSelected){
            //选择一行，高亮，解锁编辑和删除按钮
            $(this).addClass("highlight");
            toggleButtons(false);
        }
});
function jump2DetailFrame(url){
    let href = window.location.origin+"/"+ window.location.pathname.split('/')[1]+"/"+ url;
    console.log(href);
    window.location.href= href;
}